# !/usr/bin/env python
# -*- coding: utf-8 -*-


# 通过join()方法，让t1线程优先执行，t1执行完毕后，t2才执行
#
# 这种方式的缺点是把多线程变成了单线程，影响程序执行效率

import threading
import time

# 定义一个全局变量
g_num = 0


def work1():
    # 申明g_num是一个全局变量
    global g_num

    for i in range(1000000):
        g_num += 1
    print("work1------%d\n" % g_num)


def work2():
    global g_num

    for i in range(1000000):
        g_num += 1
    print("work2------%d\n" % g_num)


if __name__ == '__main__':
    t1 = threading.Thread(target=work1)
    t2 = threading.Thread(target=work2)

    t1.start()
    t1.join()
    t2.start()

    # 在子线程结束后打印g_num
    while len(threading.enumerate()) != 1:
        time.sleep(1)
    print("main------", g_num)
